home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Hot Mix 17
/
Hot Mix 17.iso
/
HM17_SGI
/
research
/
lib
/
swap_endian.pro
< prev
next >
Wrap
Text File
|
1997-07-08
|
2KB
|
69 lines
; $Id: swap_endian.pro,v 1.8 1997/01/15 03:11:50 ali Exp $
;
; Copyright (c) 1993-1997, Research Systems, Inc. All rights reserved.
; Unauthorized reproduction prohibited.
;+
; NAME:
; SWAP_ENDIAN
;
; PURPOSE:
; This fucntion reverses the byte ordering of arbitrary scalars,
; arrays or structures. It may be used, for example, to make little
; endian numbers big, or big endian numbers little.
;
; CATEGORY:
; Utility.
;
; CALLING SEQUENCE:
; Result = SWAP_ENDIAN(A)
;
; INPUTS:
; A: The scalar, array, or structure to be swapped.
;
; OUTPUTS:
; Result: The same type and structure as the input, with the
; pertinent bytes reversed.
;
; PROCEDURE:
; Swap arrays and scalars directly using BYTEORDER.
; Swap structures recursively.
;
; EXAMPLE:
; A = SWAP_ENDIAN(A) ;Reverses the "endianness" of A
;
; MODIFICATION HISTORY:
; DMS, RSI, May, 1993. Written.
; DMS, RSI, July, 1993. Added double complex.
;-
function swap_endian, in
t = in ;Make a copy
s = size(t)
case s[s[0]+1] of ;Type code
1: return, t ;don't change bytes
2: byteorder, t, /SSWAP ;shorts
3: byteorder, t, /LSWAP ;Longs
4: byteorder, t, /LSWAP ;single floats
5: BEGIN ;Double, swap longs & then swap even/odds
n = n_elements(t)
dims = size(in) ;Preserve dimensions.
t = long(temporary(t), 0, 2 * n) ;Cvt to longs
byteorder, t, /LSWAP ;swap each long
t = t[lindgen(2*n) xor 1L] ;swap pairs
t = double(temporary(t),0,n)
if dims[0] gt 0 then $ ;Return array?
return, reform(t, dims[1:dims[0]], /overwrite)
return, t[0] ;Return scalar
ENDCASE
6: byteorder, t, /LSWAP ;Complex
7: return, t ;String
8: for i=0, n_tags(t)-1 do begin
temp = swap_endian(t.(i))
t.(i) = temp
endfor
9: byteorder, t, /LSWAP ;Double complex.
ENDCASE
return, t
end